import { FormSchema, UseFormReturnType } from '/@/components/Form';
import { useForm } from '/@/components/Form';
let form: UseFormReturnType;
import { mysqlSchemas } from './mysql';
import { localSchemas } from './local';
import { ftpSchemas } from './ftp';
import { sqlliteSchemas } from './sqllite';
import { sqlserverSchemas } from './sqlserver';

enum storageType {
  file = 'file',
  mysql = 'mysql',
  sqlserver = 'sqlserver',
  sqllite = 'sqlite',
  ftp = 'ftp',
}

const baseSchemas: FormSchema[] = [
  {
    field: 'id',
    component: 'Input',
    label: 'id',
    componentProps: {
      placeholder: '请输入id',
    },
    show: false,
  },
  {
    field: 'storage_type',
    component: 'Select',
    label: '数据库类型',
    colProps: {
      span: 24,
    },
    componentProps: {
      options: [
        {
          label: 'local',
          value: storageType.file,
          key: storageType.file,
        },
        {
          label: storageType.mysql,
          value: storageType.mysql,
          key: storageType.mysql,
        },
        {
          label: storageType.sqlserver,
          value: storageType.sqlserver,
          key: storageType.sqlserver,
        },
        {
          label: storageType.sqllite,
          value: storageType.sqllite,
          key: storageType.sqllite,
        },
        {
          label: storageType.ftp,
          value: storageType.ftp,
          key: storageType.ftp,
        },
      ],
      onChange: switchType,
    },
    defaultValue: storageType.sqlserver,
  },
  {
    field: 'name',
    component: 'Input',
    label: '应用名称',
    componentProps: {
      placeholder: '请输入应用名称',
    },
    required: true,
  },
  {
    field: 'describe',
    component: 'Input',
    label: '应用描述',
    componentProps: {
      placeholder: '请输入应用描述',
    },
  },
  {
    field: 'principal',
    component: 'Input',
    label: '负责人',
    componentProps: {
      placeholder: '请输入负责人',
    },
  },
  {
    field: 'phone',
    component: 'Input',
    label: '负责人电话',
    componentProps: {
      placeholder: '请输入应用描述',
    },
  },
  {
    field: 'disable',
    component: 'Switch',
    label: '是否停用',
    defaultValue: false,
  },
];
const MYSQLDATA = [...baseSchemas, ...mysqlSchemas];
const SQLLITEDATA = [...baseSchemas, ...sqlliteSchemas];
const FTPDATA = [...baseSchemas, ...ftpSchemas];
const SQLSERVERDATA = [...baseSchemas, ...sqlserverSchemas];
const LOCALDATA = [...baseSchemas, ...localSchemas];

function switchType(stype) {
  const [_, { setProps, setFieldsValue, getFieldsValue }] = form;
  switch (stype) {
    case storageType.mysql:
      setProps({ schemas: MYSQLDATA });
      // resetSchema(MYSQLDATA);
      break;
    case storageType.sqllite:
      setProps({ schemas: SQLLITEDATA });
      // resetSchema(SQLLITEDATA);
      break;
    case storageType.ftp:
      setProps({ schemas: FTPDATA });
      // resetSchema(FTPDATA);
      break;
    case storageType.sqlserver:
      setProps({ schemas: SQLSERVERDATA });
      // resetSchema(SQLSERVERDATA);
      break;
    case storageType.file:
      setProps({ schemas: LOCALDATA });
      // resetSchema(LOCALDATA);
      break;
    default:
      setProps({ schemas: SQLSERVERDATA });
      break;
  }

  //接口类型设置默认值
  setTimeout(() => {
    const value = getFieldsValue();
    if (!value.interface_type) {
      setFieldsValue({ interface_type: 1 });
    }
  }, 200);
}

export function init() {
  const fromConfig = {
    labelCol: {
      span: 6,
    },
    wrapperCol: {
      span: 16,
    },
    schemas: SQLLITEDATA,
    showActionButtonGroup: false,
  };

  form = useForm(fromConfig);
  const [registerForm, { setFieldsValue, resetFields, validate, updateSchema }] = form;
  return { registerForm, setFieldsValue, resetFields, validate, updateSchema, switchType };
}
